#include <stdio.h>
#include "run.h"
__global__ void checkIndex(int n_boxes)
{
    int threadsPerBlock=64;
    // 获取当前线程块在二维网格中的行索引和列索引
    const int row_start = blockIdx.y;
    const int col_start = blockIdx.x;
    const int tid = threadIdx.x;
    
    // 如果行索引大于列索引，直接返回，不进行后续计算
    if (row_start > col_start) return;
    //可以看到只运行了右上角
    //printf("row_start:%d  col_start:%d\n",row_start,col_start);
    const int row_size =
        fminf(n_boxes - row_start * threadsPerBlock, threadsPerBlock);
    const int col_size =
        fminf(n_boxes - col_start * threadsPerBlock, threadsPerBlock);
    if(row_start==2&&col_start==2){
        printf("row_size:%d,col_size:%d\n",row_size,col_size);
        if(tid<col_size){
            printf("block(2,2)线程%d搬运的数据是:%d-%d\n",tid,(threadsPerBlock * col_start + tid) * 4 + 0,(threadsPerBlock * col_start + tid) * 4 + 3);
        }
    }
    // 确保所有线程都完成了数据加载
    __syncthreads();

}

int main(void)
{
    int boxes_num = 190;
    int threadsPerBlock=64;
    int col_blocks = (boxes_num + threadsPerBlock - 1) / threadsPerBlock;
    int col_blocks_alloc = GET_BLOCKS(boxes_num, threadsPerBlock);
    //int col_blocks_alloc = 2;
    printf("%d,%d,%d,%d\n",boxes_num,threadsPerBlock,col_blocks,col_blocks_alloc);
    dim3 block(col_blocks_alloc, col_blocks_alloc);
    dim3 threads(threadsPerBlock);

    printf("block: %d, %d, %d\n", block.x, block.y, block.z);
    printf("thread: %d, %d, %d\n", threads.x, threads.y, threads.z);
    //printf("grid: %d, %d, %d\n", grid.x, grid.y, grid.z);

    // //checkIndex<<<grid,block>>>();
    checkIndex<<<block,threads>>>(boxes_num);
    cudaDeviceReset();

    return 0;
}